﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;

namespace iSmartCreatorApp
{
    /// <summary>
    /// Interaction logic for TableAddWindow.xaml
    /// </summary>
    public partial class TableAddWindow : Window
    {
        //Constructor
        public TableAddWindow()
        {
            InitializeComponent();
        }

        //Button "Cancel" click
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //Button "+" for Rows click
        private void btnRowsAdd_Click(object sender, RoutedEventArgs e)
        {
            int rowsCount = Convert.ToInt32(txtbxRows.Text);
            if (rowsCount < 10)
            {
                rowsCount++;
                txtbxRows.Text = Convert.ToString(rowsCount);
            }
        }

        //Button "+" for Columns click
        private void btnColumnsAdd_Click(object sender, RoutedEventArgs e)
        {
            int columnsCount = Convert.ToInt32(txtbxColumns.Text);
            if (columnsCount < 10)
            {
                columnsCount++;
                txtbxColumns.Text = Convert.ToString(columnsCount);
            }
        }

        //Button "+" for Width click
        private void btnWidthAdd_Click(object sender, RoutedEventArgs e)
        {
            int widthCount = Convert.ToInt32(txtbxWidth.Text);
            widthCount++;
            txtbxWidth.Text = Convert.ToString(widthCount);
        }

        //Button "+" for Border click
        private void btnBorderAdd_Click(object sender, RoutedEventArgs e)
        {
            int borderCount = Convert.ToInt32(txtbxBorder.Text);
            if (borderCount < 3)
            {
                borderCount++;
                txtbxBorder.Text = Convert.ToString(borderCount);
            }
        }

        //Button "-" for Rows click
        private void btnRowsSubtract_Click(object sender, RoutedEventArgs e)
        {
            int rowsCount = Convert.ToInt32(txtbxRows.Text);
            if (rowsCount > 1)
            {
                rowsCount--;
                txtbxRows.Text = Convert.ToString(rowsCount);
            }
        }

        //Button "-" for Columns click
        private void btnColumnsSubtract_Click(object sender, RoutedEventArgs e)
        {
            int columnsCount = Convert.ToInt32(txtbxColumns.Text);
            if (columnsCount > 1)
            {
                columnsCount--;
                txtbxColumns.Text = Convert.ToString(columnsCount);
            }
        }

        //Button "-" for Width click
        private void btnWidthSubtract_Click(object sender, RoutedEventArgs e)
        {
            int widthCount = Convert.ToInt32(txtbxWidth.Text);
            if (widthCount > 1)
            {
                widthCount--;
                txtbxWidth.Text = Convert.ToString(widthCount);
            }
        }

        //Button "-" for Border click
        private void btnBorderSubtract_Click(object sender, RoutedEventArgs e)
        {
            int borderCount = Convert.ToInt32(txtbxBorder.Text);
            if (borderCount > 1)
            {
                borderCount--;
                txtbxBorder.Text = Convert.ToString(borderCount);
            }
        }

        //Button "OK" click
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            Paragraph paragraph = new Paragraph();
            FlowDocument flow = new FlowDocument();
            ArrayList list = new ArrayList();
            int rowsCount = Convert.ToInt32(txtbxRows.Text);
            int columnsCount = Convert.ToInt32(txtbxColumns.Text);
            int widthCount = Convert.ToInt32(txtbxWidth.Text);
            int borderCount = Convert.ToInt32(txtbxBorder.Text);

            string[] mas = MainWindow.HtmlText.Split('\n');
            string body = FindBody(mas);
            body += "<table style=\"text-align: left; width: " + widthCount + "px;\"" + " border=\"" + borderCount +
                    "\" cellpadding=\"2\" cellspacing=\"2\">\n";
            body += "<tbody>\n";
            for (int i = 0; i < rowsCount; i++)
            {
                body += "<tr>\n";
                for (int j = 0; j < columnsCount; j++)
                {
                    body += "<td></td>\n";
                }
                body += "</tr>\n";
            }
            body += "</tbody>\n</table>";

            MainWindow.currentRTBHtml.Document.Blocks.Clear();

            foreach (var i in mas)
            {
                list.Add(i);
            }
            list = ClearBody(list);
            list = NewBody(list, body);
            mas = (string[])list.ToArray(typeof(string));
            mas = MainWindow.GoldClear(mas);
            foreach (var i in mas)
            {
                paragraph.Inlines.Add(i + "\n");
            }
            flow.Blocks.Add(paragraph);
            MainWindow.currentRTBHtml.Document = flow;
            this.Close();
        }

        //Find Body
        private string FindBody(string[] mas)
        {
            string str = "";

            for (int i = 0; i < mas.Length; i++)
            {
                if (mas[i] == "<body>")
                {
                    i++;
                    while (mas[i] != "</body>")
                    {
                        str += mas[i] + "\n";
                        i++;
                    }
                    break;
                }
            }
            return str;
        }

        //Clear Body
        private ArrayList ClearBody(ArrayList list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ToString() == "<body>")
                {
                    i++;
                    while (list[i].ToString() != "</body>")
                    {
                        list.Remove(list[i]);
                    }
                    break;
                }
            }
            return list;
        }

        //New Body
        private ArrayList NewBody(ArrayList list, string body)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ToString() == "<body>")
                {
                    list.Insert(i + 1, body);
                    break;
                }
            }
            return list;
        }
    }
}